3 abstract class ApiTestCase
extends MediaWikiLangTestCase
{
4 protected static $apiUrl;
14 protected $tablesUsed = array( 'user', 'user_groups', 'user_properties' );
16 protected function setUp() {
20 self
::$apiUrl = $wgServer . wfScript( 'api' );
22 ApiQueryInfo
::resetTokenCache(); // tokens are invalid because we cleared the session
25 'sysop' => new TestUser(
28 'api_test_sysop@example.com',
31 'uploader' => new TestUser(
34 'api_test_user@example.com',
39 $this->setMwGlobals( array(
40 'wgMemc' => new EmptyBagOStuff(),
41 'wgAuth' => new StubObject( 'wgAuth', 'AuthPlugin' ),
42 'wgRequest' => new FauxRequest( array() ),
43 'wgUser' => self
::$users['sysop']->user
,
46 $this->apiContext
= new ApiTestContext();
50 * Edits or creates a page/revision
51 * @param string $pageName Page title
52 * @param string $text Content of the page
53 * @param string $summary Optional summary string for the revision
54 * @param int $defaultNs Optional namespace id
55 * @return array Array as returned by WikiPage::doEditContent()
57 protected function editPage( $pageName, $text, $summary = '', $defaultNs = NS_MAIN
) {
58 $title = Title
::newFromText( $pageName, $defaultNs );
59 $page = WikiPage
::factory( $title );
61 return $page->doEditContent( ContentHandler
::makeContent( $text, $title ), $summary );
65 * Does the API request and returns the result.
67 * The returned value is an array containing
68 * - the result data (array)
69 * - the request (WebRequest)
70 * - the session data of the request (array)
71 * - if $appendModule is true, the Api module $module
73 * @param array $params
74 * @param array|null $session
75 * @param bool $appendModule
76 * @param User|null $user
80 protected function doApiRequest( array $params, array $session = null,
81 $appendModule = false, User
$user = null
83 global $wgRequest, $wgUser;
85 if ( is_null( $session ) ) {
86 // re-use existing global session by default
87 $session = $wgRequest->getSessionArray();
90 // set up global environment
95 $wgRequest = new FauxRequest( $params, true, $session );
96 RequestContext
::getMain()->setRequest( $wgRequest );
98 // set up local environment
99 $context = $this->apiContext
->newTestContext( $wgRequest, $wgUser );
101 $module = new ApiMain( $context, true );
108 $module->getResultData(),
109 $context->getRequest(),
110 $context->getRequest()->getSessionArray()
113 if ( $appendModule ) {
114 $results[] = $module;
121 * Add an edit token to the API request
122 * This is cheating a bit -- we grab a token in the correct format and then
123 * add it to the pseudo-session and to the request, without actually
124 * requesting a "real" edit token.
126 * @param array $params Key-value API params
127 * @param array|null $session Session array
128 * @param User|null $user A User object for the context
129 * @return array Result of the API call
130 * @throws Exception In case wsToken is not set in the session
132 protected function doApiRequestWithToken( array $params, array $session = null,
137 if ( $session === null ) {
138 $session = $wgRequest->getSessionArray();
141 if ( isset( $session['wsToken'] ) && $session['wsToken'] ) {
142 // @todo Why does this directly mess with the session? Fix that.
143 // add edit token to fake session
144 $session['wsEditToken'] = $session['wsToken'];
145 // add token to request parameters
146 $timestamp = wfTimestamp();
147 $params['token'] = hash_hmac( 'md5', $timestamp, $session['wsToken'] ) .
148 dechex( $timestamp ) .
149 User
::EDIT_TOKEN_SUFFIX
;
151 return $this->doApiRequest( $params, $session, false, $user );
153 throw new Exception( "Session token not available" );
157 protected function doLogin( $user = 'sysop' ) {
158 if ( !array_key_exists( $user, self
::$users ) ) {
159 throw new MWException( "Can not log in to undefined user $user" );
162 $data = $this->doApiRequest( array(
164 'lgname' => self
::$users[$user]->username
,
165 'lgpassword' => self
::$users[$user]->password
) );
167 $token = $data[0]['login']['token'];
169 $data = $this->doApiRequest(
173 'lgname' => self
::$users[$user]->username
,
174 'lgpassword' => self
::$users[$user]->password
,
182 protected function getTokenList( $user, $session = null ) {
183 $data = $this->doApiRequest( array(
184 'action' => 'tokens',
185 'type' => 'edit|delete|protect|move|block|unblock|watch'
186 ), $session, false, $user->user
);
188 if ( !array_key_exists( 'tokens', $data[0] ) ) {
189 throw new MWException( 'Api failed to return a token list' );
192 return $data[0]['tokens'];
195 public function testApiTestGroup() {
196 $groups = PHPUnit_Util_Test
::getGroups( get_class( $this ) );
197 $constraint = PHPUnit_Framework_Assert
::logicalOr(
198 $this->contains( 'medium' ),
199 $this->contains( 'large' )
201 $this->assertThat( $groups, $constraint,
202 'ApiTestCase::setUp can be slow, tests must be "medium" or "large"'